﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MagicCard
{
    public partial class frmCategoryManage : Form
    {
        private string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MagicCard.accdb";
        private string type = string.Empty;

        public frmCategoryManage(string srcType)
        {
            InitializeComponent();
            type = srcType;
        }

        private bool IsDoubleName(string name)
        {
            string commStr = "select ID from MC_Category where [CategoryName] = '" + name + "'";
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                OleDbCommand comm = new OleDbCommand(commStr, conn);
                object obj = comm.ExecuteScalar();
                int count;
                if (int.TryParse(obj.ToString(), out count))
                {
                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (type == "add")
            {
                StringBuilder commStr = new StringBuilder();
                string categoryNane = txtCategoryName.Text.Trim();
                string offline = string.Empty;
                string splash = string.Empty;
                if (chkOffline.Checked)
                {
                    offline = "1";
                }
                else
                {
                    offline = "0";
                }
                if (chkSplash.Checked)
                {
                    splash = "1";
                    categoryNane += "（闪）";
                }
                else
                {
                    splash = "0";
                }
                int exp;
                if (!int.TryParse(txtExp.Text.Trim(), out exp))
                {
                    MessageBox.Show("奖励经验只能输入数字！", "错误");
                    return;
                }
                int golden;
                if (!int.TryParse(txtGolden.Text.Trim(), out golden))
                {
                    MessageBox.Show("奖励金币只能输入数字！", "错误");
                    return;
                }
                if (IsDoubleName(categoryNane))
                {
                    MessageBox.Show("套卡名称已经存在，请重新输入！", "错误");
                    return;
                }

                commStr.Append("insert into MC_Category  (CategoryName,Stars,Status,Splash,Experience,Golden)  values");
                commStr.Append("('" + categoryNane + "'");
                commStr.Append(",'" + cboStars.Text.Trim() + "'");
                commStr.Append(",'" + offline + "'");
                commStr.Append(",'" + splash + "'");
                commStr.Append(",'" + exp.ToString() + "'");
                commStr.Append(",'" + golden.ToString() + "')");

                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    conn.Open();
                    OleDbCommand comm = new OleDbCommand(commStr.ToString(), conn);
                    try
                    {
                        int ret = comm.ExecuteNonQuery();
                        if (ret == 1)
                        {
                            MessageBox.Show("套卡增加成功！", "成功");
                            txtCategoryName.Text = "";
                            txtExp.Text = "";
                            txtGolden.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("套卡增加失败！", "失败");
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }

            }
            else
            {
            }
        }
    }
}
